<?php
session_start();
include_once  (dirname(__FILE__)."../../_CONFIG/config.php");
include_once  (dirname(__FILE__)."../../_DATABASE_DAO/UserDAO.class.php");
include_once  (dirname(__FILE__)."../../_ASSETS/Mailer.php");

$userDao = new UserDAO();
$mailer = new Mailer();
if($_GET['action']=='logout'){
    session_destroy();
    include_once 'views/loginPage.html';
}elseif($_REQUEST['action']=='forget'){
    if(isset($_POST["email"])){
        $user = $userDao->forgetPassword($_POST['email']);
        $mailBody="Dear ".$user->firstName." ".$user->lastName."<br/><br/> Your Password is ".$user->password."<br/><br/> Keep it save :)";
        if($mailer->SendTaskMail(array($user), "TEDxCairo TMS Passwrd", "tedxcaior.tms@gmail.com", $mailBody))
            $error = "Password sent to ".$_POST["email"];
        else
            $error = "Error !!!";
        include_once 'views/loginPage.html';
        exit ;

    }else{
        include_once('views/forget.html');
        exit;
    }
}
if(isset($_POST['userName'])&&isset($_POST['password'])){    
	$user = $userDao->checkUser($_POST['userName'],$_POST['password']);
        $_SESSION["CURRENT_USER"] = $user;
        if($user->role->name=='ADMIN')
		die("<script type='text/javascript' >window.location.href='../admin/index.php';</script>");
	elseif($user->role->name=='REGULAR')
		die("<script type='text/javascript' >window.location.href='../user/index.php';</script>");
	else
        {
            $error= "Wrong userName or password";
            include_once 'views/loginPage.html';
        }
}
else
	include_once 'views/loginPage.html';